/**
 * tjftp -- A simple FTP server, using the ANSI C
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <linux/stddef.h>
#include <time.h>
#include "sesstion.h"
#include "lib/memory.h"
#include "lib/unity.h"
/*
 * 为了 IO 复用重入异常而设计的。
 */
static PSesstion sesstions = NULL;

PSesstion tj_sesstion_get(int id) {
	if (NULL == sesstions) {
		return NULL;
	}
	PSesstion result = sesstions;
	while (NULL != result) {
		if (result->id == id) {
			return result;
		}
		result = result->next;
	}
	return NULL;
}

void tj_sesstion_dump(int id) {
	PSesstion s = tj_sesstion_get(id);
	printf("id = %i \n", s->id);
	printf("username = %s \n", s->username);
	printf("password = %s \n", s->password);
	printf("lifetime = %u \n", s->lifetime);
	printf("is_login = %i \n", s->is_login);

	printf("root_path = %s \n", s->root_path);
	printf("pwd = %s \n", s->pwd);
	printf("permit_read = %i \n", s->permit_read);
	printf("permit_write = %i \n", s->permit_write);
	printf("permit_modify = %i \n", s->permit_modify);
	printf("permit_delete = %i \n", s->permit_delete);
	printf("is_port_mode = %i \n", s->is_port_mode);
	printf("port_address = %s \n", s->port_address);
	printf("is_pasv_mode = %i \n", s->is_pasv_mode);
}
static void _sesstion_add(PSesstion sesstion) {
	sesstion->next = sesstions;
	sesstions = sesstion;
}
PSesstion tj_sesstion_init(int id, PSettings settings) {
	PSesstion s = tj_sesstion_get(id);
	if (NULL == s) {
		s = (PSesstion) tj_memory_alloc(sizeof(Sesstion));
		s->id = id;
		memset(s->root_path, '\0', strlen(settings->document_root) + 1);
		memcpy(s->root_path, settings->document_root, strlen(settings->document_root));

		memset(s->pwd, '\0', 2);
		memcpy(s->pwd, "/", 1);
		_sesstion_add(s);
	}
	char temp[255] = { '\0' };
	sprintf(temp, "创建会话 [%i].", id);
	DEBUG_LOG(temp, 0);
	return s;
}
static void _sesstion_delete(PSesstion delete) {
	if (NULL != delete) {
		if (delete == sesstions) {
			sesstions = sesstions->next;
		} else {
			PSesstion pre = sesstions;
			while (pre->next != delete) {
				pre = pre->next;
			}
			if (delete->next == NULL) {
				pre->next = NULL;
			} else {
				pre->next = delete->next;
			}
		}
	}
	tj_memory_free(delete);
}
void tj_sesstion_destory(int id) {
	PSesstion delete = tj_sesstion_get(id);
	_sesstion_delete(delete);

	char temp[255] = { '\0' };
	sprintf(temp, "消毁会话 [%i].", id);
	DEBUG_LOG(temp, 0);
}
void tj_sesstion_destorys() {
	PSesstion current = sesstions;
	while (NULL != sesstions) {
		current = sesstions;
		sesstions = sesstions->next;
		tj_memory_free(current);
	}
}
void tj_sesstion_gc() {
	PSesstion temp = sesstions;
	while (NULL != temp) {
		if (temp->lifetime < (time(NULL) - 3600)) {
			_sesstion_delete(temp);
		}
		temp = temp->next;
	}
}
PSesstion tj_sesstion_username(int id, char* username) {
	PSesstion s = tj_sesstion_get(id);
	memcpy(s->username, username, strlen(username));
}
PSesstion tj_sesstion_password(int id, char* password) {
	PSesstion s = tj_sesstion_get(id);
	memcpy(s->password, password, strlen(password));
}
PSesstion tj_sesstion_authenticate(int id) {
	PSesstion s = tj_sesstion_get(id);
	if (!strcmp("testname", s->username) && strcmp("testpwd", s->password)) {
		s->is_login = 1;
		char root_path[] = "/home/degui/source/";
		memcpy(s->root_path, root_path, strlen(root_path));
		s->permit_read = 1;
		s->permit_write = 1;
		s->permit_modify = 1;
		s->permit_delete = 1;
	}
	return s;
}
